var moduleZTree,moduleRMenu;
var moduleTreeSetting = {	
	edit:{
		drag:{
			isCopy:false,
			isMove:true,
			prev:true,
			next:true,
			inner:false
		},
		enable:true,
		showRemoveBtn:false,
		showRenameBtn:false
	},
	data:{
		key:{
			name:"moduleName"
		},
		simpleData:{
			enable:true,
			idKey:"moduleId",
			pIdKey:"parentModuleId",
			rootPId:0
		}
	},
	callback:{
		onClick:loadModuleByModuleId,
		onRightClick:rightClick,
		beforeDrap:beforeDrag,
		beforeDrop:beforeDrop,
		onDrop:onDrop
	}
};

$(document).ready(function(){
	loadModulesData();
});

function beforeDrag(treeId,treeNodes){
	for(var i=0;i<treeNodes.length;i++){
		if(treeNodes[i].drag==false){
			return false;
		}
	}
	return true;
}

function beforeDrop(treeId,treeNodes,targetNode,moveType){
	return treeNodes[0].parentModuleId==targetNode.parentModuleId;
	//return targetNode ? targetNode.drop != false : true;
}

function onDrop(event,treeId,treeNodes,targetNode,moveType){
	var parentModuleId = treeNodes[0].parentModuleId;
	var children=new Array();
	if(parentModuleId==0){
		var nodes = moduleZTree.getNodes();
		for(var i=0;i<nodes.length;i++){
			if(nodes[i].parentModuleId==parentModuleId){
				children.push(nodes[i]);
			}
		}
	}else{
		var parentNode = moduleZTree.getNodeByParam("moduleId", parentModuleId, null);
		children = parentNode.children;
	}
	var moduleIdArray = new Array();
	for(var i=0;i<children.length;i++){
		moduleIdArray.push(children[i].moduleId);
	}
	if(moduleIdArray.length>0){
		var url = appCtx + "secure/module/updateSeqNumBatch.html";
		$.post(url,{moduleIds:moduleIdArray.join(",")});
	}
}

/**
 * 加载模块树形数据
 */
function loadModulesData(){
	var url = appCtx + "secure/module/loadModulesData.html";
	$.post(url,function(data){ 
		moduleZTree=$.fn.zTree.init($("#moduleTree"), moduleTreeSetting, data);
	});
	moduleRMenu = $("#moduleRMenu");
}

function loadModuleByModuleId(event,treeId,treeNode){
	var moduleId = treeNode.moduleId;
	refreshModuleBody(moduleId);
}

function refreshModuleBody(moduleId){
	var url = appCtx + "secure/module/queryModuleByModuleId.html";
	$.post(url,{moduleId:moduleId},function(data){ 
		$("#moduleBody").html(data);
	});
}

function rightClick(event, treeId, treeNode){
	if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
		moduleZTree.cancelSelectedNode();
		showModuleRMenu("root", event.clientX, event.clientY);
	} else if (treeNode && !treeNode.noR) {
		moduleZTree.selectNode(treeNode);
		showModuleRMenu("node", event.clientX, event.clientY);
	}
}

function showModuleRMenu(type, x, y) {
	y=y-78;
	$("#moduleRMenu ul").show();
	$("#addRootModule").show();
	$("#addModule").show();
	$("#delModule").show();
	$("#orderModule").show();
	moduleRMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

	$(".moduleView").bind("mousedown", onModuleBodyMouseDown);
}

function addRootModule(){
	hideModuleRMenu();
	var url = appCtx + "secure/module/addRootModule.html";
	$.post(url,function(data){ 
		$("#moduleBody").html(data);
	});
}

function addModule(){
	hideModuleRMenu();
	var url = appCtx + "secure/module/addModule.html";
	var nodes = moduleZTree.getSelectedNodes();
	if(nodes[0]){
		var parentModuleId = nodes[0].moduleId;
		$.post(url,{parentModuleId:parentModuleId},function(data){ 
			$("#moduleBody").html(data);
		});
	}
}

function delModule(){
	hideModuleRMenu();
	var url = appCtx + "secure/module/delete.html";
	var nodes = moduleZTree.getSelectedNodes();
	if(nodes[0]){
		var msg = "";
		var children = nodes[0].children;
		if(children && children.length>0){
			msg = "存在子模块,不允许删除!";
			$(".alert").css({"visibility":"visible","width":"130px"}).addClass("alert-block").html(msg).fadeOut(5000);
			return;
		}
		var moduleId = nodes[0].moduleId;
		var parentModuleId = nodes[0].parentModuleId;
		var moduleName = nodes[0].moduleName;
		msg = "确认删除模块［"+moduleName+"］吗?";
		if(confirm(msg)==true){
			$.post(url,{moduleId:moduleId},function(result){ 
				var flag = result.flag;
				if(flag=="success"){
					$(".alert").css("visibility","visible").addClass("alert-success").html("删除成功!");
					window.setTimeout(function(){
						refreshModuleBody(parentModuleId);
					}, 2000);
					moduleZTree.removeNode(nodes[0]);
				}else if(flag=="error"){
					$(".alert").css("visibility","visible").addClass("alert-error").html("删除失败!");
					window.setTimeout(function(){
						$(".alert").css("visibility","hidden").removeClass("alert-error")
					}, 2000);
				}
				
			});
		}
	}
}

function hideModuleRMenu() {
	if (moduleRMenu) moduleRMenu.css({"visibility": "hidden"});
	$(".moduleView").unbind("mousedown", onModuleBodyMouseDown);
}
function onModuleBodyMouseDown(event){
	if (!(event.target.id == "moduleRMenu" || $(event.target).parents("#moduleRMenu").length>0)) {
		moduleRMenu.css({"visibility" : "hidden"});
	}
}

function checkTreeNode(checked) {
	var nodes = moduleZTree.getSelectedNodes();
	if (nodes && nodes.length>0) {
		moduleZTree.checkNode(nodes[0], checked, true);
	}
	hideModuleRMenu();
}

function resetTree() {
	hideModuleRMenu();
	loadModulesData();
}

function ajaxCheck(value){
	var url = appCtx + "secure/module/checkRightCodeIsExits.html";
	var flag = false;
	var moduleId = $("#moduleId").val();
	if(moduleId!=undefined && moduleId==""){ // 如果是更新,则不检查权限编码;如果是保存,则检查
		if(value!=null && value!=""){
			$.ajax({
				type:"post",
				async:false,
				url:url,
				data:{rightCode:value}
			}).done(function(data){
				if(data==true){
					flag = true;
				}
			});
		}
	}
	return flag;
}

/**
 * 保存模块
 */
function saveModule(){
	// 验证
	var flag = $("#moduleForm").validationAjax();
	if(flag){
		// 提交
		var url = appCtx + "secure/module/save.html";
		var params = $("#moduleForm").serialize();
		$.post(
			url,
			params,
			function(result){
				var flag = result.flag;
				var module = result.module;
				var msg = "";
				if(flag=="success"){
					msg="保存成功!";
					$(".alert").css("visibility","visible").addClass("alert-success").html(msg);
					window.setTimeout(function(){
						refreshModuleBody(module.moduleId);
					}, 2000);
					var node = moduleZTree.getNodeByParam("moduleId", module.parentModuleId, null);
					var currentNode = moduleZTree.getNodeByParam("moduleId", module.moduleId, null);
					if(currentNode){
						currentNode.moduleName=module.moduleName;
						moduleZTree.updateNode(currentNode);
					}else{
						moduleZTree.addNodes(node,module);
					}
				}else if(flag=="error"){
					msg="保存失败!";
					$(".alert").css("visibility","visible").addClass("alert-error").html(msg);
					window.setTimeout(function(){
						$(".alert").css("visibility","hidden").removeClass("alert-error");
					}, 2000);
				}
			}
		);
	}
	return flag;
}